﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication1
{
    public partial class DCForm : Form
    {
        public DCForm()
        {
            InitializeComponent();
        }

        private void DCForm_Load(object sender, EventArgs e)
        {
            if (!RuningData.FormDir.ContainsKey(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name))
            {
                RuningData.FormDir.Add(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name, this);
            }

            RuningData.FoodsInit();
            treeInit();

            textBox3.Enabled = false;
            textBox1.Enabled = false;

            RuningData.WaitersInit();
            foreach (WaiterModel item in RuningData.Waiters)
            {
                comboBox1.Items.Add(item.WaiterName);
            }
            comboBox1.SelectedIndex = 0;

            flushDataView();

            RuningData.GuestFoodsInit();
        }

        private void flushDataView()
        {
            string sql = "select * from tb_GuestFood";
            DataSet ds = DatabaseHelper.GetDataSet(sql);
            dataGridView1.DataSource = ds.Tables["temp"];
        }

        private void treeInit()
        {
            string sql = "select * from tb_foodtype";
            SqlDataReader sdr = DatabaseHelper.ExecuteSelect(sql);
            if (sdr.HasRows)
            {
                while (sdr.Read())
                { 
                    TreeNode node = treeView1.Nodes.Add(sdr["foodtype"].ToString().Trim());
                    foreach(FoodModel item in RuningData.Foods)
                    {
                        if(item.FoodType.Equals(node.Text))
                        {
                            node.Nodes.Add(item.FoodName);
                        }
                    }
                }
                sdr.Close();
            }
            treeView1.ExpandAll();
        }

        private void treeView1_DoubleClick(object sender, EventArgs e)
        {
            string foodName = treeView1.SelectedNode.Text;
            if (RuningData.GetFood(foodName) != null)
            {
                string sql = string.Format("select * from tb_food where foodname='{0}'", foodName);
                SqlDataReader sdr = DatabaseHelper.ExecuteSelect(sql);
                sdr.Read();
                textBox1.Text = sdr["ID"].ToString().Trim();
                textBox2.Text = sdr["foodname"].ToString().Trim();
                textBox3.Text = sdr["foodprice"].ToString().Trim();
                sdr.Close();
            }
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            GuestFoodModel guestFood = new GuestFoodModel();
            guestFood.FoodName = textBox2.Text.Trim();
            guestFood.FoodSum = textBox3.Text.Trim();
            guestFood.FoodNum = textBox4.Text.Trim();
            guestFood.FoodAllPrice = Convert.ToDouble(textBox5.Text.Trim());
            guestFood.WaiterName = comboBox1.Text.Trim();
            guestFood.BeiZhu = textBox6.Text.Trim();
            RuningData.AddGuestFood(guestFood);
            MessageBox.Show("加入成功!");
            flushDataView();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string foodName = textBox2.Text.Trim();
            if (RuningData.DeleteGuestFood(foodName))
            {
                MessageBox.Show("删除成功!");
                string sql = "delete from tb_GuestFood where foodname = '" + foodName + "'";
                DatabaseHelper.ExecuteDelete(sql);
                flushDataView();
            }
            else
            {
                MessageBox.Show("删除失败!");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Hide();
            RuningData.FormDir["Form2"].Show();
        }

        private void textBox4_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(e.KeyChar != 8 && char.IsDigit(e.KeyChar)) && e.KeyChar != 13)
            {
                MessageBox.Show("请输入数字!");
                e.Handled = true;
            }
        }

        private void textBox4_Leave(object sender, EventArgs e)
        {
            if(textBox3.Text!=string.Empty && textBox4.Text != string.Empty)
            {
                float totalPrice = Convert.ToInt32(textBox3.Text) * Convert.ToInt32(textBox4.Text);
                textBox5.Text = totalPrice.ToString();
            }
        }
    }
}
